<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>账本管理 - 记账助手</title>
    <link rel="stylesheet" href="styles.css">
    <link href="js/bootstrap.min.css" rel="stylesheet">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.bundle.min.js"></script>
    <style>
        /* 账本管理页面样式 */
        .account-books-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            text-align: center;
            position: relative;
        }
        
        .back-btn {
            position: absolute;
            left: 20px;
            top: 50%;
            transform: translateY(-50%);
            background: rgba(255, 255, 255, 0.2);
            border: none;
            color: white;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 18px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .account-books-title {
            font-size: 20px;
            font-weight: 600;
            margin: 0;
        }
        
        .account-books-container {
            padding: 20px;
            max-width: 600px;
            margin: 0 auto;
        }
        
        .create-book-section {
            margin-bottom: 30px;
        }
        
        .create-book-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 16px;
            font-weight: 500;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
        }
        
        .create-book-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
        }
        
        .books-list {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }
        
        .book-item {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            position: relative;
            cursor: pointer;
        }
        
        .book-item:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
        }
        
        .book-item.current {
            border: 2px solid #667eea;
            background: linear-gradient(135deg, #f0f4ff 0%, #e8f2ff 100%);
        }
        
        .book-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        
        .book-info {
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .book-icon {
            width: 48px;
            height: 48px;
            border-radius: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            color: white;
        }
        
        .book-details {
            flex: 1;
        }
        
        .book-name {
            font-size: 16px;
            font-weight: 600;
            color: #2d3748;
            margin: 0 0 4px 0;
        }
        
        .book-stats {
            font-size: 12px;
            color: #718096;
        }
        
        .book-status {
            padding: 4px 8px;
            border-radius: 6px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .status-current {
            background: #667eea;
            color: white;
        }
        
        .status-normal {
            background: #f7fafc;
            color: #718096;
        }
        
        .book-actions {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .action-btn {
            width: 32px;
            height: 32px;
            border-radius: 8px;
            border: none;
            background: #f7fafc;
            color: #718096;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .action-btn:hover {
            background: #edf2f7;
            color: #4a5568;
        }
        
        /* 弹窗样式 */
        .modal-content {
            border-radius: 12px;
            border: none;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
        }
        
        .modal-header {
            border-bottom: 1px solid #e2e8f0;
            padding: 20px 24px 16px;
        }
        
        .modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #2d3748;
        }
        
        .modal-body {
            padding: 20px 24px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
            color: #4a5568;
        }
        
        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .icon-selector {
            display: grid;
            grid-template-columns: repeat(6, 1fr);
            gap: 10px;
            margin-top: 10px;
        }
        
        .icon-option {
            width: 48px;
            height: 48px;
            border-radius: 10px;
            border: 2px solid #e2e8f0;
            background: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .icon-option:hover {
            border-color: #667eea;
            transform: scale(1.05);
        }
        
        .icon-option.selected {
            border-color: #667eea;
            background: #667eea;
            color: white;
            transform: scale(1.05);
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .btn-primary:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
        }
        
        .btn-outline {
            background: white;
            border: 1px solid #e2e8f0;
            color: #4a5568;
            padding: 12px 24px;
            border-radius: 8px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .btn-outline:hover {
            border-color: #667eea;
            color: #667eea;
        }
        
        /* 操作菜单样式 */
        .action-menu {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background: white;
            border-radius: 20px 20px 0 0;
            box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
            transform: translateY(100%);
            transition: transform 0.3s ease;
            z-index: 1050;
        }
        
        .action-menu.active {
            transform: translateY(0);
        }
        
        .action-menu-header {
            padding: 20px 24px 10px;
            border-bottom: 1px solid #e2e8f0;
            text-align: center;
            position: relative;
        }
        
        .action-menu-title {
            font-size: 16px;
            font-weight: 600;
            color: #2d3748;
            margin: 0;
        }
        
        .action-menu-close {
            position: absolute;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            font-size: 20px;
            color: #718096;
            cursor: pointer;
        }
        
        .action-menu-body {
            padding: 10px 0 30px;
        }
        
        .action-menu-item {
            display: flex;
            align-items: center;
            padding: 16px 24px;
            border: none;
            background: none;
            width: 100%;
            text-align: left;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        
        .action-menu-item:hover {
            background: #f7fafc;
        }
        
        .action-menu-icon {
            width: 24px;
            height: 24px;
            margin-right: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 16px;
        }
        
        .action-menu-text {
            font-size: 16px;
            color: #2d3748;
        }
        
        .action-menu-item.danger .action-menu-icon,
        .action-menu-item.danger .action-menu-text {
            color: #e53e3e;
        }
        
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1040;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
        }
        
        .overlay.active {
            opacity: 1;
            visibility: visible;
        }
    </style>
</head>
<body>
    <div class="screen">
        <!-- 页面头部 -->
        <div class="account-books-header">
            <button class="back-btn" onclick="history.back()">
                ‹
            </button>
            <h1 class="account-books-title">账本管理</h1>
        </div>
        
        <!-- 主要内容 -->
        <div class="account-books-container">
            <!-- 创建账本按钮 -->
            <div class="create-book-section">
                <button class="create-book-btn" id="create-book-btn">
                    <span style="font-size: 20px;">+</span>
                    创建新账本
                </button>
            </div>
            
            <!-- 账本列表 -->
            <div class="books-list" id="books-list">
                <!-- 默认账本 -->
                <div class="book-item current" data-book-id="default">
                    <div class="book-header">
                        <div class="book-info">
                            <div class="book-icon">📘</div>
                            <div class="book-details">
                                <h3 class="book-name">默认账本</h3>
                                <div class="book-stats">本月支出 ¥2,580 · 收入 ¥8,500</div>
                            </div>
                        </div>
                        <div class="book-actions">
                            <span class="book-status status-current">当前使用</span>
                            <button class="action-btn" onclick="showBookActions('default', '默认账本')">
                                ⋯
                            </button>
                        </div>
                    </div>
                </div>
                
                <!-- 旅行账本 -->
                <div class="book-item" data-book-id="travel">
                    <div class="book-header">
                        <div class="book-info">
                            <div class="book-icon">✈️</div>
                            <div class="book-details">
                                <h3 class="book-name">旅行账本</h3>
                                <div class="book-stats">本月支出 ¥1,200 · 收入 ¥0</div>
                            </div>
                        </div>
                        <div class="book-actions">
                            <span class="book-status status-normal">未使用</span>
                            <button class="action-btn" onclick="showBookActions('travel', '旅行账本')">
                                ⋯
                            </button>
                        </div>
                    </div>
                </div>
                
                <!-- 家庭账本 -->
                <div class="book-item" data-book-id="family">
                    <div class="book-header">
                        <div class="book-info">
                            <div class="book-icon">🏠</div>
                            <div class="book-details">
                                <h3 class="book-name">家庭账本</h3>
                                <div class="book-stats">本月支出 ¥3,450 · 收入 ¥12,000</div>
                            </div>
                        </div>
                        <div class="book-actions">
                            <span class="book-status status-normal">未使用</span>
                            <button class="action-btn" onclick="showBookActions('family', '家庭账本')">
                                ⋯
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 创建账本弹窗 -->
    <div class="modal fade" id="createBookModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">创建新账本</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="create-book-form">
                        <div class="form-group">
                            <label class="form-label">账本名称</label>
                            <input type="text" class="form-input" id="book-name" placeholder="请输入账本名称" required>
                        </div>
                        
                        <div class="form-group">
                            <label class="form-label">选择图标</label>
                            <div class="icon-selector">
                                <div class="icon-option" data-icon="📘">📘</div>
                                <div class="icon-option" data-icon="📕">📕</div>
                                <div class="icon-option" data-icon="📗">📗</div>
                                <div class="icon-option" data-icon="📙">📙</div>
                                <div class="icon-option" data-icon="📔">📔</div>
                                <div class="icon-option" data-icon="📓">📓</div>
                                <div class="icon-option" data-icon="💼">💼</div>
                                <div class="icon-option" data-icon="🏠">🏠</div>
                                <div class="icon-option" data-icon="✈️">✈️</div>
                                <div class="icon-option" data-icon="🚗">🚗</div>
                                <div class="icon-option" data-icon="🎓">🎓</div>
                                <div class="icon-option" data-icon="💰">💰</div>
                                <div class="icon-option" data-icon="🛒">🛒</div>
                                <div class="icon-option" data-icon="🍔">🍔</div>
                                <div class="icon-option" data-icon="🎮">🎮</div>
                                <div class="icon-option" data-icon="💊">💊</div>
                                <div class="icon-option" data-icon="👶">👶</div>
                                <div class="icon-option" data-icon="🐕">🐕</div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="confirm-create-book">创建账本</button>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 编辑账本名称弹窗 -->
    <div class="modal fade" id="editBookModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">编辑账本名称</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <form id="edit-book-form">
                        <div class="form-group">
                            <label class="form-label">账本名称</label>
                            <input type="text" class="form-input" id="edit-book-name" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="confirm-edit-book">保存修改</button>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 邀请伙伴弹窗 -->
    <div class="modal fade" id="inviteModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">邀请伙伴</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="form-label">分享链接</label>
                        <div style="display: flex; gap: 10px;">
                            <input type="text" class="form-input" id="invite-link" value="https://app.example.com/invite/abc123" readonly>
                            <button type="button" class="btn btn-outline" onclick="copyInviteLink()">复制</button>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">或输入手机号邀请</label>
                        <div style="display: flex; gap: 10px;">
                            <input type="tel" class="form-input" id="invite-phone" placeholder="请输入手机号">
                            <button type="button" class="btn btn-primary" onclick="sendInvite()">发送邀请</button>
                        </div>
                    </div>
                    
                    <div style="margin-top: 20px; padding: 15px; background: #f7fafc; border-radius: 8px; font-size: 14px; color: #718096;">
                        💡 邀请伙伴后，你们可以共同管理这个账本，实时同步记账数据。
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 账本操作菜单 -->
    <div class="action-menu" id="action-menu">
        <div class="action-menu-header">
            <h5 class="action-menu-title" id="action-menu-title">账本操作</h5>
            <button class="action-menu-close" onclick="hideBookActions()">✕</button>
        </div>
        <div class="action-menu-body">
            <button class="action-menu-item" id="set-default-btn">
                <div class="action-menu-icon">⭐</div>
                <div class="action-menu-text">设为默认</div>
            </button>
            <button class="action-menu-item" id="invite-partner-btn">
                <div class="action-menu-icon">👥</div>
                <div class="action-menu-text">邀请伙伴</div>
            </button>
            <button class="action-menu-item" id="edit-name-btn">
                <div class="action-menu-icon">✏️</div>
                <div class="action-menu-text">编辑名称</div>
            </button>
            <button class="action-menu-item" id="edit-categories-btn">
                <div class="action-menu-icon">📂</div>
                <div class="action-menu-text">修改分类</div>
            </button>
            <button class="action-menu-item" id="auto-record-btn">
                <div class="action-menu-icon">⏰</div>
                <div class="action-menu-text">自动记账</div>
            </button>
            <button class="action-menu-item" id="budget-management-btn">
                <div class="action-menu-icon">💰</div>
                <div class="action-menu-text">预算管理</div>
            </button>
            <button class="action-menu-item danger" id="delete-book-btn">
                <div class="action-menu-icon">🗑️</div>
                <div class="action-menu-text">删除账本</div>
            </button>
        </div>
    </div>
    
    <!-- 遮罩层 -->
    <div class="overlay" id="overlay" onclick="hideBookActions()"></div>
    
    <script>
        let currentBookId = null;
        let currentBookName = null;
        
        // 创建账本按钮点击
        document.getElementById('create-book-btn').addEventListener('click', function() {
            const modal = new bootstrap.Modal(document.getElementById('createBookModal'));
            modal.show();
        });
        
        // 图标选择
        document.querySelectorAll('.icon-option').forEach(option => {
            option.addEventListener('click', function() {
                document.querySelectorAll('.icon-option').forEach(opt => opt.classList.remove('selected'));
                this.classList.add('selected');
            });
        });
        
        // 确认创建账本
        document.getElementById('confirm-create-book').addEventListener('click', function() {
            const bookName = document.getElementById('book-name').value.trim();
            const selectedIcon = document.querySelector('.icon-option.selected');
            
            if (!bookName) {
                showAlert('请输入账本名称');
                return;
            }
            
            if (!selectedIcon) {
                showAlert('请选择一个图标');
                return;
            }
            
            const icon = selectedIcon.getAttribute('data-icon');
            createNewBook(bookName, icon);
            
            // 关闭弹窗
            const modal = bootstrap.Modal.getInstance(document.getElementById('createBookModal'));
            modal.hide();
            
            // 重置表单
            document.getElementById('create-book-form').reset();
            document.querySelectorAll('.icon-option').forEach(opt => opt.classList.remove('selected'));
        });
        
        // 创建新账本
        function createNewBook(name, icon) {
            const bookId = 'book_' + Date.now();
            const booksList = document.getElementById('books-list');
            
            const newBook = document.createElement('div');
            newBook.className = 'book-item';
            newBook.setAttribute('data-book-id', bookId);
            newBook.innerHTML = `
                <div class="book-header">
                    <div class="book-info">
                        <div class="book-icon">${icon}</div>
                        <div class="book-details">
                            <h3 class="book-name">${name}</h3>
                            <div class="book-stats">本月支出 ¥0 · 收入 ¥0</div>
                        </div>
                    </div>
                    <div class="book-actions">
                        <span class="book-status status-normal">未使用</span>
                        <button class="action-btn" onclick="showBookActions('${bookId}', '${name}')">
                            ⋯
                        </button>
                    </div>
                </div>
            `;
            
            booksList.appendChild(newBook);
            showAlert('账本创建成功');
        }
        
        // 显示账本操作菜单
        function showBookActions(bookId, bookName) {
            currentBookId = bookId;
            currentBookName = bookName;
            
            document.getElementById('action-menu-title').textContent = bookName;
            document.getElementById('action-menu').classList.add('active');
            document.getElementById('overlay').classList.add('active');
            
            // 如果是当前账本，隐藏"设为默认"选项
            const setDefaultBtn = document.getElementById('set-default-btn');
            const currentBook = document.querySelector('.book-item.current');
            if (currentBook && currentBook.getAttribute('data-book-id') === bookId) {
                setDefaultBtn.style.display = 'none';
            } else {
                setDefaultBtn.style.display = 'flex';
            }
        }
        
        // 隐藏账本操作菜单
        function hideBookActions() {
            document.getElementById('action-menu').classList.remove('active');
            document.getElementById('overlay').classList.remove('active');
        }
        
        // 设为默认账本
        document.getElementById('set-default-btn').addEventListener('click', function() {
            if (currentBookId) {
                // 移除所有账本的current类
                document.querySelectorAll('.book-item').forEach(item => {
                    item.classList.remove('current');
                    const status = item.querySelector('.book-status');
                    status.textContent = '未使用';
                    status.className = 'book-status status-normal';
                });
                
                // 设置当前账本为默认
                const currentBook = document.querySelector(`[data-book-id="${currentBookId}"]`);
                currentBook.classList.add('current');
                const status = currentBook.querySelector('.book-status');
                status.textContent = '当前使用';
                status.className = 'book-status status-current';
                
                showAlert(`已将「${currentBookName}」设为默认账本`);
                hideBookActions();
            }
        });
        
        // 邀请伙伴
        document.getElementById('invite-partner-btn').addEventListener('click', function() {
            hideBookActions();
            const modal = new bootstrap.Modal(document.getElementById('inviteModal'));
            modal.show();
        });
        
        // 编辑账本名称
        document.getElementById('edit-name-btn').addEventListener('click', function() {
            hideBookActions();
            document.getElementById('edit-book-name').value = currentBookName;
            const modal = new bootstrap.Modal(document.getElementById('editBookModal'));
            modal.show();
        });
        
        // 确认编辑账本名称
        document.getElementById('confirm-edit-book').addEventListener('click', function() {
            const newName = document.getElementById('edit-book-name').value.trim();
            
            if (!newName) {
                showAlert('请输入账本名称');
                return;
            }
            
            if (currentBookId) {
                const book = document.querySelector(`[data-book-id="${currentBookId}"]`);
                const nameElement = book.querySelector('.book-name');
                nameElement.textContent = newName;
                
                showAlert('账本名称修改成功');
                
                // 关闭弹窗
                const modal = bootstrap.Modal.getInstance(document.getElementById('editBookModal'));
                modal.hide();
            }
        });
        
        // 修改分类
        document.getElementById('edit-categories-btn').addEventListener('click', function() {
            hideBookActions();
            // 跳转到分类管理页面
            window.location.href = 'category-management.html';
        });
        
        // 自动记账管理
        document.getElementById('auto-record-btn').addEventListener('click', function() {
            hideBookActions();
            // 跳转到自动记账管理页面
            window.location.href = 'auto-record.html';
        });
        
        // 预算管理
        document.getElementById('budget-management-btn').addEventListener('click', function() {
            hideBookActions();
            window.location.href = 'budget-management.html';
        });
        
        // 删除账本
        document.getElementById('delete-book-btn').addEventListener('click', function() {
            if (currentBookId === 'default') {
                showAlert('默认账本不能删除');
                return;
            }
            
            if (confirm(`确定要删除「${currentBookName}」账本吗？删除后数据将无法恢复。`)) {
                const book = document.querySelector(`[data-book-id="${currentBookId}"]`);
                book.remove();
                showAlert('账本删除成功');
                hideBookActions();
            }
        });
        
        // 复制邀请链接
        function copyInviteLink() {
            const linkInput = document.getElementById('invite-link');
            linkInput.select();
            document.execCommand('copy');
            showAlert('邀请链接已复制到剪贴板');
        }
        
        // 发送邀请
        function sendInvite() {
            const phone = document.getElementById('invite-phone').value.trim();
            
            if (!phone) {
                showAlert('请输入手机号');
                return;
            }
            
            if (!/^1[3-9]\d{9}$/.test(phone)) {
                showAlert('请输入正确的手机号');
                return;
            }
            
            showAlert('邀请已发送');
            document.getElementById('invite-phone').value = '';
        }
        
        // 自动记账数据
        let autoRecords = [];
        
        // 自动记账执行函数
        function executeAutoRecords() {
            const today = new Date();
            const currentDay = today.getDate();
            const currentWeekday = today.getDay(); // 0=周日, 1=周一, ..., 6=周六
            
            autoRecords.forEach(record => {
                if (!record.enabled) return;
                
                let shouldExecute = false;
                
                if (record.frequency === 'monthly') {
                    // 每月执行
                    shouldExecute = currentDay === record.day;
                } else if (record.frequency === 'weekly') {
                    // 每周执行
                    shouldExecute = currentWeekday === record.weekday;
                }
                
                if (shouldExecute) {
                    // 检查今天是否已经执行过
                    const todayStr = today.toDateString();
                    if (!record.lastExecuted || record.lastExecuted !== todayStr) {
                        // 执行自动记账
                        const recordData = {
                            type: record.type,
                            amount: record.amount,
                            category: record.category,
                            note: `${record.name} (自动记账)`,
                            date: today.toISOString().split('T')[0],
                            time: today.toTimeString().split(' ')[0].substring(0, 5)
                        };
                        
                        // 这里应该调用实际的记账API或保存到本地存储
                        console.log('执行自动记账:', recordData);
                        
                        // 更新最后执行时间
                        record.lastExecuted = todayStr;
                        
                        // 保存到本地存储
                        localStorage.setItem('autoRecords', JSON.stringify(autoRecords));
                        
                        // 显示通知
                        showAlert(`自动记账已执行: ${record.name} ${record.type === 'expense' ? '-' : '+'}¥${record.amount}`);
                    }
                }
            });
        }
        
        // 检查并执行自动记账（每小时检查一次）
        function startAutoRecordScheduler() {
            // 立即检查一次
            executeAutoRecords();
            
            // 每小时检查一次
            setInterval(executeAutoRecords, 60 * 60 * 1000);
        }
        
        // 页面加载时初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 从本地存储加载自动记账规则
            const savedRecords = localStorage.getItem('autoRecords');
            if (savedRecords) {
                autoRecords.splice(0, autoRecords.length, ...JSON.parse(savedRecords));
            }
            
            startAutoRecordScheduler();
        });
        
        // 显示提示信息
        function showAlert(message) {
            // 创建简单的提示框
            const alertDiv = document.createElement('div');
            alertDiv.style.cssText = `